package com.li.youhappygo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.youhappygo.exception.GlobalException;
import com.li.youhappygo.mapper.ProductMapper;
import com.li.youhappygo.mapper.ProductSkuMapper;
import com.li.youhappygo.model.dto.ProductDto;
import com.li.youhappygo.model.dto.ProductQueryParamDto;
import com.li.youhappygo.model.entity.product.Product;
import com.li.youhappygo.model.entity.product.ProductSku;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.model.vo.product.ProductVo;
import com.li.youhappygo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductSkuMapper productSkuMapper;

    @Override
    public PageInfo<ProductVo> getProductList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productMapper.selectList(null);
        QueryWrapper<ProductSku> queryWrapper = new QueryWrapper<>();
        List<ProductVo> productVoList = new ArrayList<>();
        for(Product product : products){
            queryWrapper.eq("product_id",product.getId());
            List<ProductSku> productSkus = productSkuMapper.selectList(queryWrapper);
            ProductVo productVo=new ProductVo();
            productVo.setProduct(product);
            productVo.setProductSkuList(productSkus);
            productVoList.add(productVo);
        }
        PageInfo<ProductVo> pageInfo1 = new PageInfo<>(productVoList);
//        List<ProductVo> list = pageInfo1.getList();
//        PageInfo<ProductVo> pageInfo2 = new PageInfo<>(list);
        return pageInfo1;
    }

    @Override
    public void save(ProductDto productDto) {
        if(productDto.getProduct() != null){
            Product product = productDto.getProduct();
            Product oldProduct = productMapper.selectOne(new QueryWrapper<Product>().lambda().eq(Product::getName, product.getName()));
            if(oldProduct == null){
                product.setStatus(1);
                product.setAuditStatus(0);
                productMapper.insert(product);
            }
        }
        List<ProductSku> productSkuList = productDto.getProductSkuList();
        if(productSkuList != null){
            if(productSkuList.size() != 0){
                for(int i =1;i<=productSkuList.size();i++) {
                    if(productSkuList.get(i - 1).getProductId() != null){
//                        productSkuList.get(i - 1).setProductId(productSkuList.get(i - 1).getProductId());
                        productSkuList.get(i - 1).setSkuId(productSkuList.get(i - 1).getProductId()+"_"+i);
                        productSkuList.get(i - 1).setSaleNum(productSkuList.get(i - 1).getSaleNum());
                        productSkuList.get(i - 1).setImgUrl(productSkuList.get(i-1).getImgUrl());
                        productSkuMapper.insert(productSkuList.get(i - 1));
                    }
                }
            }
        }


    }

    @Override
    public void updateById(ProductDto productDto) {
        Product oldProduct = productMapper.selectById(productDto.getProduct().getId());
        if(oldProduct == null){
            throw new GlobalException(ResultCode.DATA_ERROR);
        }
        Product product= productDto.getProduct();
        if(product.getName()!=null && product.getId()!=null){
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", product.getId());
            productMapper.update(product,queryWrapper);
        }
        List<ProductSku> productSkuList = productDto.getProductSkuList();
        if(productSkuList != null){
            for(int i =1;i<=productSkuList.size();i++) {
                ProductSku newProductSku = productSkuList.get(i-1);
                newProductSku.setSkuId(product.getId()+"_"+newProductSku.getId());
                QueryWrapper<ProductSku> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("id",newProductSku.getId());
                productSkuMapper.update(newProductSku,queryWrapper1);
            }
        }
    }

    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);
        QueryWrapper<ProductSku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",id);
        productSkuMapper.delete(queryWrapper);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("audit_status", auditStatus);
        productMapper.update(null, updateWrapper);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        productMapper.update(null, updateWrapper);
    }

    @Override
    public List<Product> getProductByBrandId(Long brandId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;
    }

    @Override
    public List<Product> getProductByAuditStatus(Integer audit_status) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit_status",audit_status);
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;
    }

    @Override
    public List<Product> getProductByCategoryId(Long categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id",categoryId);
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;
    }

    @Override
    public PageInfo<ProductVo> getProductListByQueryParam(ProductQueryParamDto productQueryParamDto, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(productQueryParamDto.getName()!=null,Product::getName,productQueryParamDto.getName())
                .eq(productQueryParamDto.getAuditStatus()!=null,Product::getAuditStatus,productQueryParamDto.getAuditStatus())
                .eq(productQueryParamDto.getStatus()!=null,Product::getStatus,productQueryParamDto.getStatus())
                .eq(productQueryParamDto.getBrandId()!=null,Product::getBrandId,productQueryParamDto.getBrandId())
                .eq(productQueryParamDto.getCategory1Id()!=null,Product::getCategory1Id,productQueryParamDto.getCategory1Id());

        PageHelper.startPage(pageNum,pageSize);
        List<Product> products = productMapper.selectList(queryWrapper);
        List<ProductVo> productVoList = new ArrayList<>();
        for(Product product : products){
            QueryWrapper<ProductSku> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("product_id",product.getId());
            List<ProductSku> productSkus = productSkuMapper.selectList(queryWrapper2);
            ProductVo productVo=new ProductVo();
            productVo.setProduct(product);
            productVo.setProductSkuList(productSkus);
            productVoList.add(productVo);
        }
        PageInfo<ProductVo> pageInfo = new PageInfo<>(productVoList);
//        PageInfo<Product> pageInfo = new PageInfo<>(products);
        return pageInfo;


    }

    @Override
    public void updateDeleteStatus(List<Long> ids) {
        for(Long id : ids){
            productMapper.deleteById(id);
        }
    }

    @Override
    public void updateAuditStatusByList(List<Long> ids, Integer auditStatus) {
        for(Long id : ids){
            UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id);
            updateWrapper.set("audit_status", auditStatus);
            productMapper.update(null, updateWrapper);
        }
    }
}
